package cn.apthink.calendar;

import java.time.DayOfWeek;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Week {
	private String id;
	private Day[] weekDays = new Day[7];
	
	public void setWeekDay(int index, Day day) {
		weekDays[index] = day;
	}
	
	public void setWeekDay(DayOfWeek index, Day day) {
		weekDays[(index == DayOfWeek.SUNDAY) ? 0 : index.getValue()] = day;
	}

	public Day getWeekDay(int index) {
		return weekDays[index];
	}
	
	public Day getWeekDay(DayOfWeek index) {
		return weekDays[(index == DayOfWeek.SUNDAY) ? 0 : index.getValue()];
	}

	public long duration() {
		long seconds = 0;
		for (Day day : weekDays) {
			if (Objects.isNull(day)) continue;
			seconds += day.getDuration();
		}
		return seconds;
	}

	public long durationNormal() {
		long seconds = 0;
		for (Day day : weekDays) {
			if (Objects.isNull(day)) continue;
			seconds += day.getDurationNormal();
		}
		return seconds;
	}

	public long durationOvertime() {
		long seconds = 0;
		for (Day day : weekDays) {
			if (Objects.isNull(day)) continue;
			seconds += day.getDurationOvertime();
		}
		return seconds;
	}

	@Override
	public int hashCode() {
		return Objects.hash(id);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Week other = (Week) obj;
		return Objects.equals(id, other.id);
	}
	
	public static Week testData() {
    	Week week = new Week();
        Day day = Day.testData();
        //周一至周四
        week.setWeekDay(1, day);
        week.setWeekDay(2, day);
        week.setWeekDay(3, day);
        week.setWeekDay(4, day);
        //周五
        day = new Day();
        day.addTimeRange(new TimeRange("8:00","12:00:01"));
        day.addTimeRange(new TimeRange("13:00","17:00:01"));
        week.setWeekDay(5, day);
        
        //周六周日
        week.setWeekDay(6, null);
        week.setWeekDay(0, null);
        return week;
	}
	
}
